package com.ln.kafka.v2_4_0.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <pre>
 *  kafka 客户端 多线程实例
 * </pre>
 *
 * @Author zhangj
 * @Date 2022/6/8 17:59
 */
public class ConsumerThreadSample {

    public static String TOPIC_NAME = "topic-3";

    /**
     * 经典模式：每一个线程单独创建一个kafkaconsumer，保证线程安全
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        KafkaCOnsumerRunner r1 = new KafkaCOnsumerRunner();
        Thread t1 = new Thread(r1);

        t1.start();

        Thread.sleep(15000);

        r1.shutDown();

    }

    /**
     * 为了解决kafka多线程安全问题，每一个线程的创建都会创建一个kafka消费者，保证消费者安全
     */
   static class KafkaCOnsumerRunner implements Runnable {
        private final AtomicBoolean closed = new AtomicBoolean(false);
        private final KafkaConsumer consumer;

       public KafkaCOnsumerRunner() {
           Properties props = new Properties();
           props.setProperty("bootstrap.servers", "localhost:9092");
           props.setProperty("group.id", "test");
           props.setProperty("enable.auto.commit", "false");
           props.setProperty("auto.commit.interval.ms", "1000");
           props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
           props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
           consumer = new KafkaConsumer<>(props);

           TopicPartition p0 = new TopicPartition(TOPIC_NAME, 0);
           TopicPartition p1 = new TopicPartition(TOPIC_NAME, 1);

           consumer.assign(Arrays.asList(p0, p1));
       }

       @Override
       public void run() {
            try {
                while (!closed.get()) {
                    // 处理消息
                    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
                    // 对每个partition单独处理
                    records.partitions().forEach(partition -> {
                        // 以partition获取到信息
                        List<ConsumerRecord<String, String>> pRecord = records.records(partition);
                        pRecord.forEach(record -> {
                            System.out.printf("partition = %d, offset = %d, key = %s, value = %s%n",
                                    record.partition(), record.offset(), record.key(), record.value());
                        });
                        // 实际的offset是本次处理的最后一个offset
                        long lastOffset = pRecord.get(pRecord.size() - 1).offset();
                        // 单个partition中的offset，并且进行提交
                        Map<TopicPartition, OffsetAndMetadata> offset = new HashMap<>();
                        // 下一个offset开始是 当前的offset + 1的值。
                        offset.put(partition, new OffsetAndMetadata(lastOffset + 1));
                        // 提交offset
                        consumer.commitSync(offset);
                        System.out.println("======= partition - " + partition + " end====");
                    });
                }
            } catch (Exception e) {
                if(!closed.get()) {
                    throw e;
                }
            } finally {
                consumer.close();
            }
       }


        public void shutDown() {
            closed.set(true);
            consumer.wakeup();
        }
    }

}
